package com.shakespace.artofandroid.chapter01.lifecycle

import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.shakespace.artofandroid.R
import com.shakespace.firstlinecode.global.TAG
import kotlinx.android.synthetic.main.activity_lifecycle.*

@Suppress("UNUSED_VARIABLE")
class LifecycleActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_lifecycle)
        Log.e(this.TAG, "onCreate:  ${savedInstanceState?.getString("save")} ")


        tv_system.setOnClickListener {
            System.exit(0)
        }

        tv_killer.setOnClickListener {
            android.os.Process.killProcess(android.os.Process.myPid())
        }
    }


    override fun onStart() {
        super.onStart()
        Log.e(TAG, "onStart: ")
    }

    override fun onResume() {
        super.onResume()
        Log.e(this.TAG, "onResume: ")
    }

    override fun onPause() {
        super.onPause()
        Log.e(this.TAG, "onPause: ")
    }

    override fun onStop() {
        super.onStop()
        Log.e(this.TAG, "onStop: ")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e(this.TAG, "onDestroy: ")
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putString("save", "Jack")
        Log.e(this.TAG, "onSaveInstanceState: ")
    }

    // 2020-01-06 NOTE: difference between onRestoreInstanceState and onCreate :
    // if call onRestoreInstanceState  Bundle must be not null , but in onCreate Bundle might be null
    //  onRestoreInstanceState is recommended by Google
    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        val string = savedInstanceState.getString("save")
        Log.e(this.TAG, "onRestoreInstanceState:$string ")
    }

    // 2020-01-06 NOTE: if
    @RequiresApi(Build.VERSION_CODES.O)
    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        try {
            val orientation = newConfig.orientation
            val colorMode = newConfig.colorMode
            val hardKeyboardHidden = newConfig.hardKeyboardHidden
            val locales = newConfig.locales
        } catch (e: Exception) {

        }
    }
}
